from django.template import Library

from projects import utils
from projects.models import Deliverable, Objective, Task

register = Library()

@register.inclusion_tag("deliverable_detail.html")
def show_deliverable_detail(deliverable):
  return {'deliverable_list': [deliverable]}
  
@register.inclusion_tag("task_table.html")
def list_deliverable_tasks(deliverable):
  tasks = deliverable.task_set.order_by('target_date', 'start_date')
  return {'task_list': tasks}

@register.inclusion_tag("task_table.html")
def list_complete_tasks(deliverable):
  tasks = deliverable.task.exclude(completed_date = None).order_by('target_date', 'start_date')
  return {'task_list': tasks}

@register.inclusion_tag("task_table.html")
def list_incomplete_tasks(deliverable):
  tasks = deliverable.task.filter(completed_date = None).order_by('target_date', 'start_date')
  return {'task_list': tasks}

@register.inclusion_tag("deliverable_table.html")
def list_all(deliverables = None):
  if not deliverables:
    deliverables = Deliverable.objects
  return {'deliverable_list': deliverables.order_by('target_date', 'start_date')}

@register.inclusion_tag("deliverable_table.html")
def list_incomplete_deliverables(set=None):
  '''
  List all the deliverables in a given set. If the supplied set is None then all known deliverables will be lists.
  If "incomplete" is set to True (default) then only incomplete deliverables will be displayed.
  '''
  if not set:
    set = Deliverable.objects
  return {'deliverable_list': set.filter(completed_date = None)}
  
@register.inclusion_tag("deliverable_table.html")
def list_incomplete_deliverables():
  return {'deliverable_list': Deliverable.objects.incomplete()}

@register.inclusion_tag("deliverable_table.html")
def list_overdue_deliverables():
  return {'deliverable_list': Deliverable.objects.overdue()}

@register.inclusion_tag("deliverable_table.html")
def list_todays_deliverables():
  return {'deliverable_list': Deliverable.objects.dueToday()}
  
@register.inclusion_tag("deliverable_name_full.html")
def extended_deliverable_name(deliverable):
  '''
  Display the deliverable name and hierarchy inforamtion (such as which objective it is a part of.
  '''
  return {'deliverable': deliverable}
  

@register.inclusion_tag("rating.html")
def deliverable_status(deliverable):
  assert isinstance(deliverable, Deliverable)
  incomplete_tasks = deliverable.task.filter(completed_date = None)
  incomplete_deliverables = Deliverable.objects.filter(id = deliverable.id, completed_date = None)
  objectives = Objective.objects.none()
  return utils.get_ratings("deliverable", incomplete_tasks, incomplete_deliverables, objectives)
